# This file is part of Notehaus.

# Notehaus is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.

# Notehaus is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.

# You should have received a copy of the GNU General Public License
# along with Notehaus.  If not, see <https://www.gnu.org/licenses/>.
open(FH, '<', shift);
open(OFH, '>', shift);
@LIST = (<FH>);
foreach (@LIST) {
@LIST2 = ($_);
for ($i=0; $i<=$#LIST2; $i++) {
$_ = $LIST2[$i];
while(/(?<!')\[/g) {
	$pos1 = $-[0];
	while (1) {
          /\G([^'[]|'.')*?\](?!')/gc;
          last if $pos1 != $-[0];
	  /(?<!')\[/g;
	  $pos1 = $-[0];
	}
	$pos2 = pos; # wanted value  of $+[0] stayed behind in $+[0] copy inside the lexical scope of the while block.
	@LIST2 = (@LIST2[0 .. $i], $LIST2[$i], @LIST2[$i+1 .. $#LIST2]);
        $LIST2[$i] = substr ($LIST2[$i], 0, $pos1) . substr ($LIST2[$i], $pos2);
        $LIST2[$i+1] = substr ($LIST2[$i+1], 0, $pos1) . substr ($LIST2[$i+1], $pos1+1, $pos2-1-($pos1+1)) . substr ($LIST2[$i+1], $pos2);
	$_=$LIST2[$i];
}
}
  print OFH @LIST2;
}
close FH;
close OFH;
